package com.foo.ui_demo_widge.adapter;

import android.content.Context;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.ui_demo_widget1.R;

import java.util.List;
import java.util.Random;

public class MyRecycleViewAdapter extends RecyclerView.Adapter
        <MyRecycleViewAdapter.MyViewHolder> {
        private List<String> data;
        private Context context;

    public MyRecycleViewAdapter(List<String> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @NonNull

    @Override
    public MyViewHolder onCreateViewHolder(@NonNull  ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item, parent, false);

        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull  MyViewHolder holder, int position) {
        holder.getTv_char().setText(data.get(position));
        Random random=new Random();
        int rand_height= (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,48+random.nextInt(20)*5,context.getResources().getDisplayMetrics());
        holder.getTv_char().getLayoutParams().height=rand_height;
        holder.getTv_char().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context,holder.getTv_char().getText().toString(),Toast.LENGTH_SHORT).show();

            }
        });

    }

    @Override
    public int getItemCount() {
        return 0;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder{
        private TextView tv_char;

        public TextView getTv_char() {
            return tv_char;
        }

        public void setTv_char(TextView tv_char) {
            this.tv_char = tv_char;
        }

        public MyViewHolder(@NonNull  View itemView) {
            super(itemView);
             tv_char = itemView.findViewById(R.id.tv_char);

        }
    }
}
